package com.bobi.tfca.web.exception.advice;


import com.bobi.tfca.model.pojo.R;
import com.bobi.tfca.utils.MessageUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Controller全局异常处理
 * 统一返回格式为 R
 */

@RestControllerAdvice
@Log4j2
public class ControllerExceptionAdvice {

    private static final R<?> FAIlED_RESULT = R.failed("操作异常");


    /**
     * 全局异常，统一处理接口处理过程中的异常，统一返回
     * @param exception 异常信息
     * @return 接口统一的返回结果
     */
    @ExceptionHandler(value = Exception.class)
    public R<?> errorHandler(Exception exception) {
        log.error(MessageUtils.getErrMsg(exception));
        return FAIlED_RESULT;
    }

    /**
     * 自定义异常
     *
     * @param exception 自定义异常
     * @return R 通用返回格式
     */
    @ExceptionHandler(value = CustomException.class)
    public R<?> errorHandler(CustomException exception) {
        return R.failed(exception.getMessage());
    }
}
